﻿using Snake.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Snake
{
    public partial class Frm_Ranking : Form
    {
        int barid = 0;

        public Frm_Ranking()
        {
            InitializeComponent();
        }
        public Frm_Ranking(int id)
        {
            InitializeComponent();
            barid=id;
            var rankingdata = HttpRequest.GetRanking(id);
            AnalizeRankingstr(rankingdata);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btn_return_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void AnalizeRankingstr(List<string> rank)
        {
            List<RankList> rankLists = new List<RankList>();
            foreach(var p in rank)
            {
                if (p == "") break;
                var vs = p.Split('|');
                rankLists.Add(new RankList(barid, int.Parse(vs[0]), int.Parse(vs[1]), int.Parse(vs[2]), int.Parse(vs[3])));
            }
            rankLists.Sort(new ScoreComparer());

            int index = 1;
            foreach(var p in rankLists)
            {
                lbx_ranking.Items.Add((index++).ToString("00")+"                   " + p.ToString());
            }
        }

        public class ScoreComparer : IComparer<RankList>
        {
            int IComparer<RankList>.Compare(RankList x, RankList y)
            {
                if (x.Score > y.Score) return -1;
                else if (x.Score < y.Score) return 1;
                else return 0;
            }
        }
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        private void Frm_Ranking_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        protected override void OnShown(EventArgs e)
        {
            Graphics g = CreateGraphics();
            g.DrawRectangle(new Pen(Brushes.White, 3f), 0, 0, Width - 3, Height - 3);
        }
    }
}
